package xcmg.device.web.split;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.split.SplitService;
import xcmg.device.service.vo.split.SplitAddVO;
import xcmg.device.service.vo.split.SplitArrivalVO;
import xcmg.device.service.vo.split.SplitPdaVO;
import xcmg.device.service.vo.split.SplitVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.Arrays;

@RestController
@RequestMapping("/split")
public class SplitController extends BasicController {

    @Autowired
    private SplitService splitService;

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<SplitVO> condition)throws Exception{
        SplitVO splitVO = condition.getFilters();
        splitVO.setOrgId(getOrgId());
        condition.setFilters(splitVO);
        PageCommonVO pageCommonVO = splitService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/getSplitAmount",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult getSplitAmount(@RequestBody SplitVO condition)throws Exception{
        return actionValueResult(splitService.getSplitAmount(condition, getOrgId()));
    }

    @RequestMapping(value = "/detailList",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult detailList(@RequestBody SplitVO condition)throws Exception{
        return actionValueResult(splitService.detailList(condition, getOrgId()));
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult add(@RequestBody SplitAddVO splitAddVO)throws Exception{
        SplitVO splitVO = splitAddVO.getHeaderInfo();
        splitVO.setCreateId(getUserId());
        splitVO.setOrgId(getOrgId());
        splitVO.setCreateName(getUserName());
        splitAddVO.setHeaderInfo(splitVO);
        try {
            return actionResult(splitService.add(splitAddVO));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/item",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult item(String id){
        return actionValueResult(splitService.item(id));
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult update(@RequestBody SplitAddVO splitAddVO)throws Exception{
        try {
            return actionResult(splitService.update(splitAddVO));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult remove(@RequestBody String[] ids)throws Exception{
        if(ids==null||ids.length==0){
            return actionResult(ErrorCode.IllegalArument);
        }
        try {
            return actionResult(splitService.delete(Arrays.asList(ids), getOrgId()));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/splitPda",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult splitPda(@RequestBody SplitPdaVO splitPdaVO)throws Exception{
        try {
            return actionResult(splitService.splitPda(splitPdaVO, getUserId(), getOrgId()));
        }catch (Exception e){
            getExceptionAllinformation(e);
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/audit",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult audit(@RequestBody SplitVO splitVO)throws Exception{
        try {
            return actionResult(splitService.audit(splitVO, getUserId(), getUserName()));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/export",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public void export( String[] ids)throws Exception {
        splitService.export(Arrays.asList(ids));
    }

    @RequestMapping(value = "/exportAll",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public void exportAll(SplitVO condition)throws Exception {
        condition.setOrgId(getOrgId());
        splitService.exportAll(condition);
    }

    @RequestMapping(value = "/arrivalList",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult arrivalList(@RequestBody SearchCommonVO<SplitArrivalVO> condition)throws Exception{
        SplitArrivalVO splitVO = condition.getFilters();
        splitVO.setOrgId(getOrgId());
        condition.setFilters(splitVO);
        PageCommonVO pageCommonVO = splitService.arrivalList(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/exportArrivalAll",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public void exportArrivalAll(SplitArrivalVO condition)throws Exception {
        condition.setOrgId(getOrgId());
        splitService.exportArrivalAll(condition);
    }

    private String getExceptionAllinformation(Exception ex){
        String sOut = "\r\n";
        sOut += ex.getMessage();
        StackTraceElement[] trace = ex.getStackTrace();
        for (StackTraceElement s : trace) {
            sOut += "\tat " + s + "\r\n";
        }
        return sOut;
    }
}
